package com.mjh.zhujianju.controller.party;

import com.livew.framework.base.controller.BaseController;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.base.service.FileService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedResult;
import com.livew.framework.pojo.AjaxResult;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.party.BuildingNewsEO;
import com.mjh.zhujianju.service.base.ContentService;
import com.mjh.zhujianju.service.party.BuildingNewsService;
import com.mjh.zhujianju.vo.party.BuildingNewsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Alex
 * @version: V1.0
 * @project: zhujianju
 * @package: com.mjh.zhujianju.controller.party
 * @description: description
 * @date: 2018/8/16
 * @time: 下午4:32
 */
@Controller()
@RequestMapping("party/news")
public class NewsController extends BaseController<BuildingNewsEO>{

    @Autowired
    private BuildingNewsService buildingNewsService;

    @Autowired
    private FileService fileService;

    @Autowired
    private ContentService contentService;


    @Override
    protected BaseService<BuildingNewsEO> getService() {
        return buildingNewsService;
    }

    @RequestMapping(value="saveNewsInfo",method=RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveNewsInfo(BuildingNewsVO buildingNewsVO) {
        if(buildingNewsVO==null) {
            return AjaxResult.getResult(Boolean.FALSE, "参数错误" );
        }
        BuildingNewsEO buildingNewsEO = new BuildingNewsEO();
        BeanUtils.copyProperties(buildingNewsVO,buildingNewsEO);
        if(StringUtils.isEmpty(buildingNewsEO.getId())){
            try {
                buildingNewsService.saveSelective(buildingNewsEO);
                if(!StringUtils.isEmpty(buildingNewsVO.getFiles())){
                    fileService.updateList(buildingNewsVO.getFiles(), buildingNewsEO.getId(), BuildingNewsEO.class.getName());
                }

                if (buildingNewsVO.getBasicFunction() != null) {
                    contentService.saveOrUpdate(BusinessEnum.TYPE_BUILDING.getCode(), buildingNewsEO.getId(), buildingNewsEO.getBasicFunction());
                }
            } catch (XTBusinessException e) {
                e.printStackTrace();
                return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
            }
        }else {
            try {
                buildingNewsService.updateByPrimaryKey(buildingNewsEO);
                if(!StringUtils.isEmpty(buildingNewsVO.getFiles ())){
                    fileService.updateList(buildingNewsVO.getFiles(), buildingNewsEO.getId(), BuildingNewsEO.class.getName());
                }

                // 更新内容
                contentService.saveOrUpdate(BusinessEnum.TYPE_BUILDING.getCode(), buildingNewsEO.getId(), buildingNewsEO.getBasicFunction());
            } catch (XTBusinessException e) {
                e.printStackTrace();
                return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
            }
        }

        return AjaxResult.getResult(Boolean.TRUE, "保存成功");
    }

    @RequestMapping(value = "getPage", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getPage(BuildingNewsVO buildingNewsVO) {
        PagedResult<BuildingNewsVO> list = buildingNewsService.findBuildingNewsPagination(buildingNewsVO);
        return AjaxResult.getResult(list);
    }

    @RequestMapping(value = "editNews/{id}", method = RequestMethod.GET)
    public String editNews(@PathVariable("id") Long id, Model model) {
        BuildingNewsVO buildingNewsVO = null;
        try {
            buildingNewsVO = buildingNewsService.queryBuildingNewsInfo(id);
        } catch (XTBusinessException e) {
            e.printStackTrace();
        }
        model.addAttribute("buildingNews", buildingNewsVO);
        return "party/news/create";
    }

    @RequestMapping(value = "detailNews/{id}", method = RequestMethod.GET)
    public String detailNews(@PathVariable("id") Long id, Model model) {
        BuildingNewsVO buildingNewsVO = null;
        try {
            buildingNewsVO = buildingNewsService.queryBuildingNewsInfo(id);
        } catch (XTBusinessException e) {
            e.printStackTrace();
        }
        model.addAttribute("buildingNews", buildingNewsVO);
        return "party/news/detail";
    }

    @Override
    public AjaxResult findById(@PathVariable("id") Long id) {
        if (id == null) {
            return AjaxResult.getResult(Boolean.FALSE, "参数错误");
        }
        BuildingNewsVO t = null;
        try {
            t =this.buildingNewsService.getInfo(id);
        } catch (XTBusinessException e) {
            e.printStackTrace();
            return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
        }
        return AjaxResult.getResult(t);
    }
}
